import { createRouter, createWebHistory } from "vue-router";
import { ElMessage } from "element-plus";
import Login from "../views/Login.vue";
import Register from "../views/Register.vue";
import Home from "../views/Home.vue";
import Category from "@/views/Category.vue";
import User from "@/views/userPages/User.vue";
import UserAbout from "@/views/userPages/UserAbout.vue";
import UserInformation from "@/views/userPages/UserInformation.vue";
import UserHealth from "@/views/userPages/UserHealth.vue";
import UserAccount from "@/views/userPages/UserAccount.vue";
import MedicalReport from "@/views/MedicalReport.vue";
import About from "@/views/About.vue";
import { useTokenStore } from "@/stores/token";
import Index from "@/views/Index.vue";
import FoodDetail from '../views/FoodDetail.vue'
import Family from "@/views/userPages/Family.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login,
    },
    {
      path: "/register",
      name: "register",
      component: Register,
    },
    {
      path: "/",
      name: "home",
      component: Home,
      redirect: "/index",
      children: [
        {
          path: "index",
          name: "index",
          component: Index,
        },
        {
          path: "category",
          name: "category",
          component: Category,
          meta: {
            keepAlive: true
          }
        },
        {
          path: "medicalReport",
          name: "medicalReport",
          component: MedicalReport,
        },
        {
          path: "about",
          name: "about",
          component: About,
        }
      ],
    },
    {
      path: "/user",
      name: "user",
      component: User,
      redirect: "/user/about",
      meta: { title: "账号中心" },
      children: [
        {
          path: "about",
          name: "userAbout",
          component: UserAbout,
        },
        {
          path: "information",
          name: "userInformation",
          component: UserInformation,
        },
        {
          path: "health",
          name: "userHealth",
          component: UserHealth,
        },
        {
          path: "account",
          name: "userAccount",
          component: UserAccount,
        },
        {
          path: "family",
          name: "userFamily",
          component: Family,
        }
      ],
    },
    {
      path: '/food/:id',
      name: 'FoodDetail',
      component: FoodDetail
    },
    {
      path: '/food-compare',
      name: 'FoodCompare',
      component: () => import('@/views/FoodCompare.vue')
    }
  ],
});

// 不需要登录就可以访问的路由
const publicRoutes = [
  "/login",
  "/register",
  "/",
  "/category",
  "/medicalReport",
  "/index",
  "/about",
];

router.beforeEach((to, from, next) => {
  const tokenStore = useTokenStore();
  const token = tokenStore.getToken();

  // 设置页面标题
  document.title = to.meta.title || "膳养智选";

  // 如果没有token且不是公开路由，重定向到登录页
  if (!token && !publicRoutes.includes(to.path)) {
    ElMessage.warning("请先登录");
    next("/login");
    return;
  }

  next();
});

export default router;
